//
//  UIBarButtonItem+BaseFramework.m
//  Project
//
//  Created by Suny on 14-11-18.
//  Copyright (c) 2014年 Suny. All rights reserved.
//

#import "UIBarButtonItem+BaseFramework.h"
#import "Button.h"
#import "UIColor+BaseFramework.h"

#define kMinimumButtonWidth		50
#define kTitlePadding			10

#define kTagCustomButton        10

typedef NS_ENUM(NSInteger , BarButtonItemType)
{
    BarButtonItemTypeDefault = 0,
    BarButtonItemTypeRight,
    BarButtonItemTypeLeft,
} ;

@implementation UIBarButtonItem (BaseFramework)

#pragma mark - New

+ (UIBarButtonItem *)rightBarButtonItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector
{
	Button *rightBtn = [Button buttonWithType:UIButtonTypeCustom];
	[rightBtn setFrame:CGRectMake(0, 0, 54, 44)];
	[rightBtn setTitle:title forState:UIControlStateNormal];
	[rightBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
	[rightBtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
	[rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[rightBtn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	UIView *background = [[UIView alloc] initWithFrame:rightBtn.frame];
	background.tag = kNavigationItemAlignment_HR_VC;
	[background addSubview:rightBtn];
	return [[UIBarButtonItem alloc] initWithCustomView:background];
}


+ (UIBarButtonItem *)rightBarButtonItemWithTitle:(NSString *)title titleColor:(UIColor *)titleColor target:(id)target selector:(SEL)selector
{
	Button *rightBtn = [Button buttonWithType:UIButtonTypeCustom];
	[rightBtn setFrame:CGRectMake(0, 0, 54, 44)];
	[rightBtn setTitle:title forState:UIControlStateNormal];
	[rightBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
	[rightBtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
	[rightBtn setTitleColor:titleColor forState:UIControlStateNormal];
	[rightBtn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	UIView *background = [[UIView alloc] initWithFrame:rightBtn.frame];
	background.tag = kNavigationItemAlignment_HR_VC;
	[background addSubview:rightBtn];
	return [[UIBarButtonItem alloc] initWithCustomView:background];
}

+ (UIBarButtonItem *)leftBarButtonItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector
{
	Button *rightBtn = [Button buttonWithType:UIButtonTypeCustom];
	[rightBtn setFrame:CGRectMake(0, 0, 54, 44)];
	[rightBtn setTitle:title forState:UIControlStateNormal];
	[rightBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
	[rightBtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
	[rightBtn setTitleColor:[UIColor colorWithHex:0xf84c4b] forState:UIControlStateNormal];
	[rightBtn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	UIView *background = [[UIView alloc] initWithFrame:rightBtn.frame];
	background.tag = kNavigationItemAlignment_HL_VC;
	[background addSubview:rightBtn];
	return [[UIBarButtonItem alloc] initWithCustomView:background];
}

+ (UIBarButtonItem *)backBarButtonItemWithTarget:(id)target selector:(SEL)selector
{
	UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, kDefaultNavigationBarHeight)];
	bgView.tag = kNavigationItemAlignment_HL_VC;
	bgView.backgroundColor = [UIColor clearColor];
	
	Button *button = [Button buttonWithType:UIButtonTypeCustom];
	button.frame = bgView.bounds;
	button.tag = kTagCustomButton;
	[button setImage:[UIImage imageNamed:@"navigationbar_back_icon.png"] forState:UIControlStateNormal];
	[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	[bgView addSubview:button];
	
	UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:bgView];
	
	return barButtonItem;
}


+ (UIBarButtonItem *)refreshBarButtonItemWithTarget:(id)target selector:(SEL)selector
{
	UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 46, kDefaultNavigationBarHeight)];
	button.tag = kNavigationItemAlignment_HR_VC;
	[button setImage:[UIImage imageNamed:@"navigationbar_refresh_icon.png"] forState:UIControlStateNormal];
	[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	
	UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return barButtonItem;
}

+ (UIBarButtonItem *)leftBarButtonItemWithImageName:(NSString *)imageName
							   highlightedImageName:(NSString *)highlightedImageName
											 target:(id)target
										   selector:(SEL)selector
{
	UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 46, kDefaultNavigationBarHeight)];
	button.tag = kNavigationItemAlignment_HL_VC;
	[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
	[button setImage:[UIImage imageNamed:highlightedImageName] forState:UIControlStateHighlighted];
	[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	
	UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return barButtonItem;
}

+ (UIBarButtonItem *)rightBarButtonItemWithImageName:(NSString *)imageName
								highlightedImageName:(NSString *)highlightedImageName
											  target:(id)target
											selector:(SEL)selector
{
	UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 46, kDefaultNavigationBarHeight)];
	button.tag = kNavigationItemAlignment_HR_VC;
	[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
	[button setImage:[UIImage imageNamed:highlightedImageName] forState:UIControlStateHighlighted];
	[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	
	UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return barButtonItem;
}

+ (UIBarButtonItem *)leftBarButtonItemWithImageName:(NSString *)imageName
											 target:(id)target
										   selector:(SEL)selector
{
	UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, kDefaultNavigationBarHeight)];
	bgView.tag = kNavigationItemAlignment_HL_VC;
	bgView.backgroundColor = [UIColor clearColor];
	
	Button *button = [Button buttonWithType:UIButtonTypeCustom];
	button.frame = bgView.bounds;
	[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
	[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	[bgView addSubview:button];
	
	UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:bgView];
	
	return barButtonItem;
}

+ (UIBarButtonItem *)leftBarButtonItemWithImageName:(NSString *)imageName
											 target:(id)target
										   selector:(SEL)selector
										  applySkin:(BOOL)applySkin
{
	UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 46, kDefaultNavigationBarHeight)];
	button.tag = kNavigationItemAlignment_HL_VC;
	if (applySkin) {
		[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
	}
	else {
		[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
	}
	[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	
	UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return barButtonItem;
}

+ (UIBarButtonItem *)rightBarButtonItemWithImageName:(NSString *)imageName
											  target:(id)target
											selector:(SEL)selector
{
	UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 46, kDefaultNavigationBarHeight)];
	button.tag = kNavigationItemAlignment_HR_VC;
	[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
	[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	
	UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return barButtonItem;
}

+ (UIBarButtonItem *)rightBarButtonItemWithImageName:(NSString *)imageName
								   selectedImageName:(NSString *)selectedImageName
											  target:(id)target
											selector:(SEL)selector
{
	UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 46, kDefaultNavigationBarHeight)];
	button.tag = kNavigationItemAlignment_HR_VC;
	[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
	[button setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected];
	[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	
	UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return barButtonItem;
}

- (UIButton *)customButton {
	return (UIButton *)[self.customView viewWithTag:kTagCustomButton];
}


@end
